home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / pcap-snoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-31  |  5.0 KB  |  196 lines

  1. /*
  2.  * Copyright (c) 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static  char rcsid[] =
  23.     "@(#)$Header: pcap-snoop.c,v 1.6 94/01/31 05:26:09 leres Exp $ (LBL)";
  24. #endif
  25.  
  26. #include <sys/param.h>
  27. #include <stdio.h>
  28. #include <netdb.h>
  29. #include <ctype.h>
  30. #include <signal.h>
  31. #include <errno.h>
  32. #include <sys/time.h>
  33. #include <sys/socket.h>
  34. #include <sys/file.h>
  35. #include <sys/ioctl.h>
  36.  
  37. #include <net/raw.h>
  38.  
  39. #include <net/if.h>
  40. #include <netinet/in.h>
  41. #include <netinet/in_systm.h>
  42. #include <netinet/ip.h>
  43. #include <netinet/if_ether.h>
  44. #include <netinet/ip_var.h>
  45. #include <netinet/udp.h>
  46. #include <netinet/udp_var.h>
  47. #include <netinet/tcp.h>
  48. #include <netinet/tcpip.h>
  49. #include <net/bpf.h>
  50.  
  51. #include "pcap-int.h"
  52.  
  53. static int hdrpad;                    /* XXX */
  54.  
  55. int
  56. pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  57. {
  58.     int cc;
  59.     register struct snoopheader *sh;
  60.     register int datalen;
  61.     register int caplen;
  62.     register u_char *cp;
  63.  again:
  64.     cc = read(p->fd, (char *)p->buffer, p->bufsize);
  65.     if (cc < 0) {
  66.         switch (errno) {
  67.         case EWOULDBLOCK:
  68.             return (0);            /* XXX */
  69.         }
  70.         sprintf(p->errbuf, "read: %s", pcap_strerror(errno));
  71.         return (-1);
  72.     }
  73.     sh = (struct snoopheader *)p->buffer;
  74.     datalen = sh->snoop_packetlen;
  75.     caplen = (datalen < p->snapshot) ? datalen : p->snapshot;
  76.     cp = (u_char *)(sh + 1) + hdrpad;        /* XXX */
  77.  
  78.     if (p->fcode.bf_insns == NULL ||
  79.         bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
  80.         struct pcap_pkthdr h;
  81.         ++p->md.stat.ps_recv;
  82.         h.ts = sh->snoop_timestamp;
  83.         h.len = datalen;
  84.         h.caplen = caplen;
  85.         (*callback)(user, &h, cp);
  86.         return (1);
  87.     }
  88.     return (0);
  89. }
  90.  
  91. int
  92. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  93. {
  94.     register struct rawstats *rs;
  95.     struct rawstats rawstats;
  96.  
  97.     rs = &rawstats;
  98.     bzero((char *)rs, sizeof(*rs));
  99.     if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
  100.         sprintf(p->errbuf, "SIOCRAWSTATS: %s", pcap_strerror(errno));
  101.         return (-1);
  102.     }
  103.  
  104.     p->md.stat.ps_drop =
  105.         rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
  106.         rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops;
  107.  
  108.     *ps = p->md.stat;
  109.     return (0);
  110. }
  111.  
  112. /* XXX can't disable promiscuous */
  113. pcap_t *
  114. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  115. {
  116.     pcap_t *p;
  117.     struct sockaddr_raw sr;
  118.     int fd;
  119.     int v;
  120.     struct snoopfilter sf;
  121.  
  122.     p = (pcap_t *)malloc(sizeof(*p));
  123.     if (p == NULL) {
  124.         strcpy(ebuf, "no swap");
  125.         return (0);
  126.     }
  127.     bzero(p, sizeof(*p));
  128.     p->fd = -1;
  129.     p->bufsize = 4096;                /* XXX */
  130.     p->buffer = (u_char *)malloc(p->bufsize);
  131.     if (p->buffer == NULL) {
  132.         strcpy(ebuf, "no swap");
  133.         goto bad;
  134.     }
  135.     fd = p->fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
  136.     if (fd < 0) {
  137.         sprintf(ebuf, "snoop socket: %s", pcap_strerror(errno));
  138.         goto bad;
  139.     }
  140.     sr.sr_family = AF_RAW;
  141.     sr.sr_port = 0;
  142.     (void)strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
  143.     if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {
  144.         sprintf(ebuf, "snoop bind: %s", pcap_strerror(errno));
  145.         goto bad;
  146.     }
  147.     bzero((char *)&sf, sizeof(sf));
  148.     if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {
  149.         sprintf(ebuf, "SIOCADDSNOOP: %s", pcap_strerror(errno));
  150.         goto bad;
  151.     }
  152.     v = 64 * 1024;
  153.     (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v));
  154.     if (ioctl(fd, SIOCSNOOPLEN, &snaplen) < 0) {
  155.         sprintf(ebuf, "SIOCSNOOPLEN: %s", pcap_strerror(errno));
  156.         goto bad;
  157.     }
  158.     p->snapshot = snaplen;
  159.     v = 1;
  160.     if (ioctl(fd, SIOCSNOOPING, &v) < 0) {
  161.         sprintf(ebuf, "SIOCSNOOPING: %s", pcap_strerror(errno));
  162.         goto bad;
  163.     }
  164.     /*
  165.      * XXX hack - map device name to link later type
  166.      */
  167.     if (strncmp("et", device, 2) == 0 ||
  168.         strncmp("ec", device, 2) == 0) {
  169.         p->linktype = DLT_EN10MB;
  170.         hdrpad = RAW_HDRPAD(sizeof(struct ether_header));
  171.     } else if (strncmp("ipg", device, 3) == 0 ||
  172.            strncmp("xpi", device, 3) == 0) {
  173.         p->linktype = DLT_FDDI;
  174.         hdrpad = 3;                /* XXX yeah? */
  175.     } else {
  176.         sprintf(ebuf, "snoop: unknown physical layer type");
  177.         goto bad;
  178.     }
  179.     return (p);
  180.  bad:
  181.     if (fd >= 0)
  182.         close(fd);
  183.     if (p->buffer != NULL)
  184.         free(p->buffer);
  185.     free(p);
  186.     return (0);
  187. }
  188.  
  189. int
  190. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  191. {
  192.  
  193.     p->fcode = *fp;
  194.     return (0);
  195. }
  196.